CC = arm-none-eabi-gcc
BIN := RTOSDemo.axf

BUILD_DIR := build

FREERTOS_DIR := $(CURDIR)/FreeRTOS

SOURCE_FILES += init/startup.c  syscall.c main.c
SOURCE_FILES += $(FREERTOS_DIR)/portable/GCC/ARM_CM3/port.c
SOURCE_FILES += $(FREERTOS_DIR)/tasks.c
SOURCE_FILES += $(FREERTOS_DIR)/list.c
SOURCE_FILES += $(FREERTOS_DIR)/queue.c
SOURCE_FILES += $(FREERTOS_DIR)/timers.c
SOURCE_FILES += $(FREERTOS_DIR)/event_groups.c
SOURCE_FILES += ${FREERTOS_DIR}/portable/MemMang/heap_3.c

INCLUDE_DIRS += -I$(CURDIR)
INCLUDE_DIRS += -I$(CURDIR)/CMSIS

INCLUDE_DIRS += -I$(FREERTOS_DIR)/Demo/CORTEX_M3_MPS2_QEMU_GCC
INCLUDE_DIRS += -I$(FREERTOS_DIR)/include
INCLUDE_DIRS += -I$(FREERTOS_DIR)/portable/GCC/ARM_CM3

ifeq ($(FULL_DEMO), 1)
    SOURCE_FILES += main_full.c
    SOURCE_FILES += $(FREERTOS_DIR)/stream_buffer.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/AbortDelay.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/BlockQ.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/blocktim.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/countsem.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/death.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/dynamic.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/EventGroupsDemo.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/flop.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/GenQTest.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/integer.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/IntSemTest.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/MessageBufferAMP.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/MessageBufferDemo.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/PollQ.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/QPeek.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/QueueOverwrite.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/QueueSet.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/QueueSetPolling.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/recmutex.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/semtest.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/StaticAllocation.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/StreamBufferDemo.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/StreamBufferInterrupt.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/TaskNotify.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Minimal/TimerDemo.c
    SOURCE_FILES += ${FREERTOS_DIR}/Demo/Full/print.c

    INCLUDE_DIRS += -I$(FREERTOS_DIR)/Demo/include
    INCLUDE_DIRS += -I$(FREERTOS_DIR)/Demo/include

    CFLAGS := -DmainCREATE_FULL_DEMO_ONLY=1
else
    SOURCE_FILES += main_blinky.c

    CFLAGS := -DmainCREATE_SIMPLE_BLINKY_DEMO_ONLY=1
endif

#DEFINES := -DprojCOVERAGE_TEST -DQEMU_SOC_MPS2 -DHEAP3
DEFINES :=  -DQEMU_SOC_MPS2 -DHEAP3

LDFLAGS = -T ./scripts/mps2_m3.ld -specs=nano.specs --specs=rdimon.specs -lc -lrdimon
LDFLAGS += -Xlinker -Map=${BUILD_DIR}/output.map

CFLAGS += -nostartfiles -mthumb -mcpu=cortex-m3 -Wno-error=implicit-function-declaration
CFLAGS += -Wno-builtin-declaration-mismatch -Werror

ifeq ($(DEBUG), 1)
    CFLAGS += -ggdb3 -Og
else
    CFLAGS += -O3
endif
    CFLAGS += -fstrict-aliasing -Wstrict-aliasing -Wno-error=address-of-packed-member

OBJ_FILES := $(SOURCE_FILES:%.c=$(BUILD_DIR)/%.o)

CPPFLAGS += $(DEFINES)
CFLAGS += $(INCLUDE_DIRS)

.PHONY: clean

$(BUILD_DIR)/$(BIN) : $(OBJ_FILES)
	$(CC) -ffunction-sections -fdata-sections $(CFLAGS) $(LDFLAGS) $+ -o $(@)

%.d: %.c
	@set -e; rm -f $@; \
	$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

INCLUDES := $(SOURCE_FILES:%.c=$(BUILD_DIR)/%.d)
-include $(INCLUDES)

${BUILD_DIR}/%.o : %.c Makefile
	-mkdir -p $(@D)
	$(CC) $(CFLAGS) $(CPPFLAGS) -MMD -c $< -o $@

clean:
	-rm -rf build

